//
//  CustomTabbarVC.m
//  小钱钱
//
//  Created by sdzhqy on 2017/3/20.
//  Copyright © 2017年 sdzhqy. All rights reserved.
//

#import "CustomTabbarVC.h"
#import "LCPanNavigationController.h"
#import "mybabyVC.h"
#import "messageVC.h"
#import "accountVC.h"
#import "common.h"
@interface CustomTabbarVC ()
@property(nonatomic,strong)NSMutableArray *imgViewList;//按钮图片列表
@property(nonatomic,strong)NSMutableArray *labViewList;//按钮文字列表

@end

@implementation CustomTabbarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadSet];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self _removeSystemBarbtn];
    self.navigationController.navigationBarHidden=YES;//隐藏系统的导航栏
}
//去除系统的tabbar的按钮
- (void)_removeSystemBarbtn{
    
    Class class =  NSClassFromString(@"UITabBarButton");
    
    for (UIView *view in self.tabBar.subviews){
        
        //如果view是由UITabBarButton类创建的对象，则将其移除
        if ([view isKindOfClass:class]){
            [view removeFromSuperview];
        }
    }
}
//自定义tabbar按钮
-(void)loadSet
{
    //设置tabbar背景颜色
    self.tabBar.backgroundColor=[UIColor whiteColor];
    self.tabBar.alpha=1;
    //第一页
    mybabyVC *firstVC=[[mybabyVC alloc]init];
    LCPanNavigationController *firstNav=[[LCPanNavigationController alloc]initWithRootViewController:firstVC];
    firstNav.tabBarItem.tag=0;
    //第二页
    messageVC *secondVC=[[messageVC alloc]init];
    LCPanNavigationController *secondNav=[[LCPanNavigationController alloc]initWithRootViewController:secondVC];
    secondNav.tabBarItem.tag=1;
    //第三页
    accountVC *thirdVC=[[accountVC alloc]init];
    LCPanNavigationController *thirdNav=[[LCPanNavigationController alloc]initWithRootViewController:thirdVC];
    thirdNav.tabBarItem.tag=2;
    NSMutableArray *list=[[NSMutableArray alloc]initWithObjects:firstNav,secondNav,thirdNav, nil];
    self.viewControllers=list;
    //设置自图片与按钮
    _imgViewList=[[NSMutableArray alloc]init];
    _labViewList=[[NSMutableArray alloc]init];
    float width=kScreenWidth/3;
    float height=self.tabBar.frame.size.height;
    NSArray *titles=@[@"我的宝贝",@"动态消息",@"我的账户"];
    for (int i=0; i<3; i++) {
        //背景图片
        UIImageView *myImageView=[[UIImageView alloc]init];
        myImageView.userInteractionEnabled=YES;
        myImageView.contentMode=UIViewContentModeScaleAspectFit;
        UIImageView *bgmView=[[UIImageView alloc]init];
        UILabel *bgmlab=[[UILabel alloc]init];
        bgmlab.font=[UIFont systemFontOfSize:11];
        bgmlab.textAlignment=NSTextAlignmentCenter;
        bgmlab.textColor=RGB_UI_COLOR(159, 159, 159);
        [myImageView addSubview:bgmlab];
       // [myImageView addSubview:bgmView];
        [self.tabBar addSubview:myImageView];
        //按钮
        UIButton* myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [myBtn addTarget:self action:@selector(bottomBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:myBtn];
        [bgmView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"home_icon%d.png",i+1]]];
        [bgmView setHighlightedImage:[UIImage imageNamed:[NSString stringWithFormat:@"home_icon%d_down.png",i+1]]];
        NSString *title = [titles objectAtIndex:i];
        [myImageView setFrame:CGRectMake(width*i, 0, width, height)];
        bgmView.frame=CGRectMake((myImageView.size.width-30)/2, 5, 30, 30);
        bgmlab.text=title;
        bgmlab.textAlignment=NSTextAlignmentCenter;
        bgmlab.frame=CGRectMake(0, bgmView.bottom-5, myImageView.size.width, 20);
        [_imgViewList addObject:bgmView];
        [_labViewList addObject:bgmlab];
        myBtn.tag = i;
        [myBtn setFrame:myImageView.frame];
    }
    [self liSelectIndex:0];
}
//按钮点击
-(void)bottomBtnPressed:(UIButton*)sender
{
    
    [self liSelectIndex:(int)sender.tag];
}
//实现切换
- (void)liSelectIndex:(int)index
{
    NSLog(@"点击：%d",index);
    self.selectedIndex = index;
    for(UIImageView *imgView in _imgViewList)
    {
        [imgView setHighlighted:NO];
    }
    UIImageView *imgView = [_imgViewList objectAtIndex:index];
    [imgView setHighlighted:YES];
    for (UILabel *lab in _labViewList) {
        lab.textColor=RGB_UI_COLOR(159, 159, 159);
    }
    UILabel *lab=[_labViewList objectAtIndex:index];
    lab.textColor=RGB_UI_COLOR(255, 145, 1);
}
    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 0;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
